package com.lovegu.framework.mybatis.dataSource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

/**
 * @author 大妖怪 9920664@qq.com
 */
@Repository
public class TenantDataSourceRepository extends ServiceImpl<TenantDataSourceMapper, TenantDataSourceDO> {

    /**
     * 查询单条
     *
     * @param dto
     * @return
     */
    public TenantDataSourceDO findOne(FindTenantDataSourceDTO dto) {
        QueryWrapper<TenantDataSourceDO> queryWrapper = getQueryWrapper(dto);
        return baseMapper.selectOne(queryWrapper);
    }

    private QueryWrapper<TenantDataSourceDO> getQueryWrapper(FindTenantDataSourceDTO dto) {
        QueryWrapper<TenantDataSourceDO> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<TenantDataSourceDO> lambda = queryWrapper.lambda();
        lambda.eq(dto.getTenantId() != null, TenantDataSourceDO::getTenantId, dto.getTenantId());
        return queryWrapper;
    }
}
